package com.aaa.tars.controller;

import com.aaa.tars.entity.Pay;
import com.aaa.tars.entity.Refund;
import com.aaa.tars.service.RefundService;
import com.aaa.tars.util.*;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

/**
 * @FileName: RefundController
 * @Description:
 * @Author: ghz
 * @CreateTime:2025/9/12 14:22
 * @Version:1.0.0
 */
@RestController
@RequestMapping("refund") // 注意：路径无斜杠（之前代码是"refund"，非"/refund"，避免路径匹配问题）
public class RefundController {
    @Resource
    private RefundService refundService;

    // 1. 查询单个退款记录（保持不变）
    @GetMapping("queryById")
    public Result queryById(Integer refundId) {
        try {
            Refund refund = refundService.queryById(refundId);
            return ResultUtil.success(refund);
        } catch (Exception e) {
            return ResultUtil.error("查询退款记录失败：" + e.getMessage());
        }
    }

    // 2. 分页查询退款记录（保持不变）
    @PostMapping("queryByPage")
    public Result queryByPage(@RequestBody Page<Refund> page) {
        try {
            PageInfo<Refund> pageInfo = refundService.queryByPage(page);
            return ResultUtil.success(pageInfo);
        } catch (Exception e) {
            return ResultUtil.error("分页查询退款记录失败：" + e.getMessage());
        }
    }

    // 3. 新增退款记录（核心修复：彻底移除refundSerialno校验）
    @PostMapping("insert") // 路径为"refund/insert"，无斜杠
    @Transactional
    public Result insert(@RequestBody Refund refund) {
        // 关键：仅打印必要日志，不校验refundSerialno
        System.out.println("Controller接收到的订单号：" + refund.getOrderNo());
        System.out.println("Controller接收到的退款金额：" + refund.getRefundIje());
        System.out.println("Controller接收到的退款原因：" + refund.getRefundFullmsg());

        try {
            // 直接调用Service，由Service生成流水号（不再拦截）
            int rows = refundService.insert(refund);
            // 打印Service生成的流水号（验证结果）
            System.out.println("Service生成的退款流水号：" + refund.getRefundSerialno());
            return ResultUtil.success( refund);
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常堆栈，便于排查
            return ResultUtil.error("退款申请提交失败：" + e.getMessage());
        }
    }

    // 4. 修改退款记录（保持不变）
    @PutMapping("update")
    @Transactional
    public Result update(@RequestBody Refund refund) {
        try {
            int rows = refundService.update(refund);
            return ResultUtil.success(rows);
        } catch (Exception e) {
            return ResultUtil.error("退款记录修改失败：" + e.getMessage());
        }
    }

    // 5. 删除退款记录（保持不变）
    @DeleteMapping("deleteById")
    public Result deleteById(Integer refundId) {
        try {
            int rows = refundService.deleteById(refundId);
            return ResultUtil.success( rows);
        } catch (Exception e) {
            return ResultUtil.error("退款记录删除失败：" + e.getMessage());
        }
    }
    
    // 6. 根据订单号查询退款记录
    @PostMapping("queryByOrderNo")
    public Result queryByOrderNo(@RequestBody Refund refund) {
        try {
            if (refund.getOrderNo() == null || refund.getOrderNo().trim().isEmpty()) {
                return ResultUtil.error("订单号不能为空");
            }
            
            List<Refund> refunds = refundService.queryByOrderNo(refund.getOrderNo());
            return ResultUtil.success(refunds);
        } catch (Exception e) {
            return ResultUtil.error("查询退款记录失败：" + e.getMessage());
        }
    }
}